"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : SQL_DDL_Drop_Owned
Case Name   : name参数无效值验证，合理报错
Create At   : 2022/08/23
Owner       : @li-xin12345
Description :
    1.删除初始用户所拥有的数据库对象
    2.name为不存在的用户的
    3.name参数包含特殊字符@*等
    4.name参数为空字符串
    5.name参数为纯数字
Expect      :
    1.合理报错
    2.合理报错
    3.合理报错
    4.合理报错
    5.合理报错
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class DropOwned(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.user_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.user_name_1 = 'u_drop_owned_0002'
        self.user_name_2 = 'u@drop_owned_0002'
        self.user_name_3 = 'u*drop_owned_0002'

    def test_drop_owned(self):
        text = "-----step1:删除初始用户所拥有的数据库对象;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"drop owned by {self.user_node.ssh_user};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(f'ERROR:  cannot drop objects owned by role '
                      f'{self.user_node.ssh_user} because they are '
                      f'required by the database system',
                      sql_msg, '执行失败' + text)

        text = "-----step2:name为不存在的用户的;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"drop owned by {self.user_name_1};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(f'ERROR:  role "{self.user_name_1}" does not exist',
                      sql_msg, '执行失败' + text)

        text = "-----step3:name参数包含特殊字符@*等;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"drop owned by {self.user_name_2};" \
            f"drop owned by {self.user_name_3};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertEquals(2, sql_msg.count(self.constant.SYNTAX_ERROR_MSG),
                          '执行失败' + text)

        text = "-----step4:name参数为空字符串;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"drop owned by '';"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SYNTAX_ERROR_MSG, sql_msg, '执行失败' + text)

        text = "-----step5:name参数为纯数字，expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"drop owned by 0002;"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SYNTAX_ERROR_MSG, sql_msg, '执行失败' + text)

    def tearDown(self):
        self.logger.info("-----无需清理环境-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
